package leetcode.offer0029;

import leetcode.IDebug;
import leetcode.helper.H;
import leetcode.helper.tree.Node;
import leetcode.helper.tree.NodeArray;

public class Solution implements IDebug {

	public Node insert(Node head, int insertVal) {
		Node insert = new Node(insertVal);
		if (head == null) {
			head = insert;
			head.next = head;
			return head;
		}
		Node cur = head.next, last = head;
		while (cur != head) {
			if ((cur.val > last.val && insertVal <= cur.val && insertVal >= last.val)
					|| (cur.val < last.val && (insertVal >= last.val || insertVal <= cur.val))) {
				last.next = insert;
				insert.next = cur;
				return head;
			}
			last = cur;
			cur = cur.next;
		}
		last.next = insert;
		insert.next = cur;
		return head;
	}

	@Override
	public void debug4() {
		Node head = new Node(3);
		head.next = new Node(5);
		head.next.next = new Node(1);
		head.next.next.next = head;
		this.insert(head, 0);
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
